/**
 * @author Cian
 */

package cian_f_rice.controller;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import cian_f_rice.model.DecisionLinesEvent;
import cian_f_rice.model.Edge;
import cian_f_rice.view.DecisionLinesGUI;

public class DrawEdgeController extends MouseAdapter {

	DecisionLinesGUI view;
	DecisionLinesEvent model;
	
	public DrawEdgeController(DecisionLinesEvent model, DecisionLinesGUI view)
	{
		this.view = view;
		this.model = model;
	}
	
	/**
	 * This is the method that communicates the clicking of a mouse with adding an edge.
	 * Basically, it detects if the Y value of the mouse is greater than 10 (the value where lines
	 * start) it goes through a for loop to see if it's between the current edge and an edge above it.
	 * If the edge is the size of the Lines ArrayList - 1 then an edge can't be added to the right of it (as 
	 * array Lists start at 0
	 * 
	 * Then if the edge is successfully added the canvas is repainted and the status set to nothing.
	 * If not, then an error message is displayed.
	 */
	
	public void mouseClicked(MouseEvent me) {
		
		if (view.getAddEdges())
		{
			
			int width = view.getDrawingPanel().getWidth()/model.numChoices;
			int choiceWidth = width-20;
			
			if (me.getY() > 10)
			{
				for (int i = 0; i < model.numChoices; i++)
				{		
					if  (i < model.numChoices - 1)
					{
						if (me.getX() >= (i*width + choiceWidth/2)
								&& me.getX() <= (i+1)*width + choiceWidth/2)
							{
								Edge addedEdge = new Edge(i, i + 1 , me.getY());
							
								if (model.addEdge(addedEdge))
								{
									view.getDrawingPanel().repaint();
									int edgesRemaining = (model.numChoices * model.getNumRounds()) - model.getAllEdges().size();
									view.setStatus("Edges Remaining: " +  edgesRemaining);
									return;
								}
								else
								{
									view.setStatus("Invalid Edge!");
									return;
								}
						}
					}
				}
			}

		}
	}
	
}
